/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.snmputils.main;

import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 *
 * @author Rainbow
 */
public class MainController
{
    public static void main(String[] args)
    {
        System.out.println("com.seari.snmputils.main.MainController.main()");
        Address targetAddress = GenericAddress.parse("udp:192.168.90.2/161");   
  
        CommunityTarget target = new CommunityTarget();   
        target.setCommunity(new OctetString("public"));   
        target.setAddress(targetAddress);   
        target.setTimeout(10000);   
        target.setVersion(SnmpConstants.version1);   
        // creating PDU   
        PDU pdu = new PDU();   
     
        pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3")));     
        pdu.setType(PDU.GET);
        System.out.println("request UDP:" + pdu);
  
        try {   
            TransportMapping transport = new DefaultUdpTransportMapping();    
            Snmp snmp = new Snmp(transport);   
            snmp.listen() ;
            ResponseEvent responseEvent = snmp.send(pdu, target);   
            PDU response = null;//定义response包
            response = responseEvent.getResponse();//接收response PDU
            if (response!=null)
            	System.out.println(response);
  
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }
}
